package com.clh.seckill.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.clh.seckill.mapper.SeckillOrderMapper;
import com.clh.seckill.pojo.Order;
import com.clh.seckill.mapper.OrderMapper;
import com.clh.seckill.pojo.SeckillGoods;
import com.clh.seckill.pojo.SeckillOrder;
import com.clh.seckill.pojo.User;
import com.clh.seckill.service.IOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.clh.seckill.service.ISeckillGoodsService;
import com.clh.seckill.vo.GoodsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author clh
 * @since 2021-11-12
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {
    @Autowired
    ISeckillGoodsService seckillGoodsService;
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    SeckillOrderMapper seckillOrderMapper;
    @Autowired
    RedisTemplate redisTemplate;

    @Transactional
    @Override
    public Order createSeckillOrder(User user, GoodsVo goodsVo) {
        //减少库存
        ValueOperations valueOperations = redisTemplate.opsForValue();
        SeckillGoods seckillGoods = seckillGoodsService.getOne(new QueryWrapper<SeckillGoods>().eq("goods_id", goodsVo.getId()));
        Integer count=seckillGoods.getSeckillCount();
//        if (count<1){
//            return null;
//        }
        seckillGoods.setSeckillCount(--count);
//        seckillGoodsService.updateById(seckillGoods);
        //用sql的方式解决库存超卖的问题
        boolean isok=seckillGoodsService.update(new UpdateWrapper<SeckillGoods>().setSql(
                "seckill_count=seckill_count-1")
                .eq("id",seckillGoods.getId()).gt("seckill_count",0));
        if (!isok){
            valueOperations.set("isStockEmpty:"+goodsVo.getId(),true);
            return null;
        }
        //生成订单
        Order order =new Order();
        order.setUserId(user.getId());
        order.setGoodsId(goodsVo.getId());
        order.setDeliveryAddrId(0L);
        order.setGoodsName(goodsVo.getGoodsName());
        order.setGoodsPrice(seckillGoods.getSeckillPrice());
        order.setGoodsCont(1);
        order.setOrderChannel(1);
        order.setStatus(0);
        order.setCreateDate(new Date());
        orderMapper.insert(order);
        //再生成秒杀订单
        SeckillOrder seckillOrder = new SeckillOrder();
        seckillOrder.setUserId(user.getId());
        seckillOrder.setGoodsId(goodsVo.getId());
        seckillOrder.setOrderId(order.getId());
        seckillOrderMapper.insert(seckillOrder);
        //将订单信息存入到redis中去
        redisTemplate.opsForValue().set("order:"+user.getId()+":"+goodsVo.getId(),seckillOrder,5, TimeUnit.MINUTES);
        return order;
    }
}
